<script src="<?php echo base_url(); ?>public/js/jquery.js"></script>

<?php echo $this->validation->error_string; ?>
<?php if(isset($error)) echo "<p>$error</p>"; ?>
<?php
	$attributes = array('name'=>'survey');
	echo form_open("respondents/respond/".$link, $attributes);

	if($firstname!=0)
	{
		echo "First Name ".form_input("firstname",$this->validation->firstname)."<br>";
	}
	if($middlename!=0)
	{
		echo "Middle Name ".form_input("middlename",$this->validation->middlename)."<br>";
	}
	if($lastname!=0)
	{
		echo "Last Name ".form_input("lastname",$this->validation->lastname)."<br>";
	}
	if($gender!=0)
	{
		echo "Gender";
		$selected_gender = $this->validation->gender;
		$data = array(
		'name'        => 'gender',
		'id'          => 'gender',
		'value'       => 'M',
		'checked'     => $selected_gender=='M' ? TRUE : FALSE,
		'style'       => 'margin:10px',
		);
		echo form_radio($data)."M";
	
		$data = array(
		'name'        => 'gender',
		'id'          => 'gender',
		'value'       => 'F',
		'checked'     => $selected_gender=='F' ? TRUE : FALSE,
		'style'       => 'margin:10px',
		);
		echo form_radio($data)."F<br>";
	}
	
	if($birthday!=0)
	{
		echo "Birthday ";
		echo $formdate->selectMonth();
		echo $formdate->selectDay(); 
		echo $formdate->selectYear();  
		echo "<br>";
	}
	
	if($location!=0)
	{
		?>
		<script type="text/javascript">
			function loadLocation()
			{
				var e = document.getElementById("location");
				var chosen = e.options[e.selectedIndex].text;
				if(chosen == "Philippines")
				{
					document.getElementById("regionLabel").innerHTML = "Region ";
					document.getElementById("provinceLabel").innerHTML = "Province ";
					document.getElementById("cityLabel").innerHTML = "City ";
					document.getElementById("othersLabel").innerHTML = "";
					document.getElementById("region").style.visibility = "visible";
					document.getElementById("province").style.visibility = "visible";
					document.getElementById("city").style.visibility = "visible";
					document.getElementById("others").type = "hidden";
				}
				else if (chosen == "")
				{
					document.getElementById("regionLabel").innerHTML = "";
					document.getElementById("provinceLabel").innerHTML = "";
					document.getElementById("cityLabel").innerHTML = "";
				
	document.getElementById("othersLabel").innerHTML = "";
					document.getElementById("region").style.visibility = "hidden";
					document.getElementById("others").type = "hidden";
					document.getElementById("province").style.visibility = "hidden";
					document.getElementById("city").style.visibility = "hidden";
		
		}
				else
				{
					document.getElementById("regionLabel").innerHTML = "";
					document.getElementById("provinceLabel").innerHTML = "";
					document.getElementById("cityLabel").innerHTML = "";
					document.getElementById("othersLabel").innerHTML = "City ";
					document.getElementById("region").style.visibility = "hidden";
					document.getElementById("others").type = "text";
					document.getElementById("province").style.visibility = "hidden";
					document.getElementById("city").style.visibility = "hidden";
				}
			}
			
			function getXMLHTTP() { //fuction to return the xml http object
				var xmlhttp=false;	
				try{
					xmlhttp=new XMLHttpRequest();
				}
				catch(e)	{		
					try{			
						xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e){
						try{
						xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
						}
						catch(e1){
						
	xmlhttp=false;
						}
					}
				}
				 	
				return xmlhttp;
			}
	
	
			function getProvince(strURL) {		
		
				var req = getXMLHTTP();
		
				if (req) {
			
					req.onreadystatechange = function() {
						if (req.readyState == 4) {
							// only if "OK"
							if (req.status == 200) {
							
	document.getElementById('provinceDiv').innerHTML=req.responseText;						
							} else {
								alert("There was a problem while using XMLHTTP:\n" + req.statusText);
							}
						}				
				
	}			
					req.open("GET", strURL, true);
					req.send(null);
				}
				
			}
						
			function getCity(strURL) {		
				var req = getXMLHTTP();
				if (req) {
			
					req.onreadystatechange = function() {
						if (req.readyState == 4) {
							// only if "OK"
							if (req.status == 200) {
									document.getElementById('cityDiv').innerHTML=req.responseText;						
							} else {
								alert("There was a problem while using XMLHTTP:\n" + req.statusText);
							}
						}				
					}			
					req.open("GET", strURL, true);
					req.send(null);
			
	}
				
			}
		</script>
		<?php
		
		$js = "id=\"location\" onChange =\"loadLocation();\"" ;
		$options = array("" => "", "Philippines"=> "Philippines" , "Others" => "Others");
		echo "Country ".form_dropdown("country", $options, $this->validation->country, $js)."<br>"; 
		?>
		<text id="othersLabel"></text>
		<input type="hidden" id="others" name="others" value="<?php $this->validation->others ?>" />
		<?php
		$js = "id=\"region\" onChange=\"getProvince('http://localhost/pollwatcher/getprovince.php?region='+this.value);\"";
		$options =  array(""=>"");
		$regions  = array_merge($options, $regions);
		echo "<text id=\"regionLabel\"></text><br>";
		echo form_dropdown("region", $regions, $this->validation->region, $js)."<br>"; 
		
		$js = "id=\"province\" onChange=\"getCity('http://localhost/pollwatcher/getcity.php?province='+this.value);\"";
		echo "<text id=\"provinceLabel\"></text><div id=\"provinceDiv\">".form_dropdown("province",array("" => "") , $this->validation->province, $js)."</div>";
		
		echo "<text id=\"cityLabel\"></text>";
		$js = "id=\"city\"" ;
		echo "<div id=\"cityDiv\">".form_dropdown("city", array("" => ""), $this->validation->city, $js)."</div><br>"; 
		echo "<div id=\"testDiv\"></div>";
		
		?>
		
		<script type="text/javascript">
		onload = function(){
			loadLocation();
			
			if(<?php echo "\"".$this->validation->country."\""; ?>=="Philippines")
			{
				getProvince('http://localhost/pollwatcher/getprovince.php?region='+<?php echo "\"".$this->validation->region."\"";?>+'&province='+<?php echo "\"".$this->validation->province."\"";?>);
				getCity('http://localhost/pollwatcher/getcity.php?province='+<?php echo "\"".$this->validation->province."\"";?>+'&city='+<?php echo "\"".$this->validation->city."\"";?>);
			}
		}
		
		document.getElementById("region").style.visibility = "hidden";
		document.getElementById("province").style.visibility = "hidden";
		document.getElementById("city").style.visibility = "hidden";
		</script>
		<?php
	
	}
?>
<h2><?php echo $title;?></h2>

<?php
echo $description;
$count = 1;
$initially_empty = FALSE;

if(sizeof($categories) == 0)
{
	$categories = array();
	$initially_empty = TRUE;
	$pages = ceil(sizeof($questions)/5);	
	for($i = 0; $i < sizeof($questions); $i++)
	{
		if($i % 5 == 0)
		{
			$page = ceil(($i+1)/5);
			$categories[] = array("category_name" => "Part $page of $pages");
		}		
		$questions[$i]['category_name'] = "Part $page of $pages";
	}
}

foreach($categories as $category)
{
	$question_number = 1;
	if($count==1)
	{
		echo "<div id=\"category$count\">";
		
	}
	else
	{
		echo "<div id=\"category$count\" style=\"display:none\">";
	}
	echo "<h3>".$category['category_name']."</h3>";
	
	$count++;
		
	foreach($questions as $item)
	{
		if($item['category_name'] == $category['category_name'])
		{
			$temp = "question".$item['question_number'];
			echo $question_number++.". ".$item['body']."<br/>";
			if($item['type'] == "text")
			{		
				$data = array(
				      'name'        => "question".$item['question_number'],
				      'id'          => "question".$item['question_number'],
				      'value'       => $this->validation->$temp,
				      'maxlength'   => '100',
				      'size'        => '50',
				      'style'       => 'width:50%;margin:10px',
				    );

				echo form_input($data);
				echo "<br/>";
			}
	
	
			if($item['type'] == "paragraph text")
			{
				$data = array(
				      'name'        => "question".$item['question_number'],
				      'id'          => "question".$item['question_number'],
				      'value'      => $this->validation->$temp,
				      'rows'   => '3',
				      'style'       => 'width:50%;margin:10px',
				    );
				echo form_textarea($data);
			}
	
			if($item['type'] == "multiple choice")
			{
				foreach($item['choices'] as $choice)
				{
					$checked = FALSE;
					if($this->validation->$temp==$choice)
					{
						$checked = TRUE;
					}
					$data = array(
					'name'        => "question".$item['question_number'],
					'id'          => "question".$item['question_number'],
					'value'       => $choice,
					'checked'     => $checked,
					'style'       => 'margin:10px',
					);

					echo form_radio($data). $choice."<br/>";
				}
			}
	
			if($item['type'] == "checkboxes")
			{
				?>
				<script type="text/javascript">
					function chkcontrol(j,limit)
					{
						var total=0;
						for(var i=0; i < document.survey.<?php echo "question".$item['question_number']; ?>.length; i++)			{
							if(document.survey.<?php echo "question".$item['question_number']; ?>[i].checked){
								total =total +1;}
								if(total > limit){
								document.survey.<?php echo "question".$item['question_number']; ?>[j].checked = false ;
					
			return false;
							}
						}
					}
				</script>
				<?php
				$temp = "question".$item['question_number'];
				$checked = $this->validation->$temp;
				for($i=1; $i<=sizeof($item['choices']);$i++)
				{
					$selected = FALSE;
					if($checked!=FALSE)
					{
						$selected = in_array($item['choices'][$i], $checked);
					}
					$js = "onClick=\"chkcontrol(".($i-1).",".$item['choice_limit'].","."question".$item['question_number'].")\";";
					$data = array(
					'name'        => "question".$item['question_number']."[]",
		
			'id'          => "question".$item['question_number'],
					'value'       => $item['choices'][$i],
					'checked'     => $selected,
					'style'       => 'margin:10px',
					);

					echo form_checkbox($data,NULL, FALSE, $js ). $item['choices'][$i]."<br/>";
				}
			}
	
			if($item['type'] == "choose")
			{
				$js = "id="."question".$item['question_number'];
				$choices = array(""=>"");
				foreach($item['choices'] as $choice)
				{
					$choices[$choice] = $choice;
				}
				echo "&nbsp;&nbsp;&nbsp;".form_dropdown("question".$item['question_number'], $choices, $this->validation->$temp, $js);
			}
	
			if($item['type'] == "scale")
			{
				echo "<br>";
	
			for($i = 1; $i<=strlen($item['choices'][1]) + 10; $i++)
				{
					echo "&nbsp;";
				}
		
				for($choice = 1; $choice<=5; $choice++)
				{
					echo $choice."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				echo "<br/>";
			
				echo "&nbsp;&nbsp;&nbsp;".$item['choices'][1]."&nbsp;&nbsp;&nbsp;&nbsp;";
	
			for($choice = 1; $choice<=5; $choice++)
				{
					$data = array(
					'name'        => "question".$item['question_number'],
					'id'          => "question".$item['question_number'],
					'value'       => $choice,
					'checked'     => $this->validation->$temp == $choice ? TRUE : FALSE,
					);

					echo form_radio($data)."&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				echo $item['choices'][2]."<br>";
			}
	
			if($item['type'] == "ranking")
			{
				echo "<table colspan=\"2\"><tr><td><table>";
				for($i=1; $i<=sizeof($item['choices']);$i++)
				{
					echo "<tr><td>".$i."</td></tr>";
				}
				echo "</table></td><td>";
		
				echo "<table id=\"question".$item['question_number']."\">";
		
				for($i=1; $i<=sizeof($item['choices']);$i++)
				{
					$temp = "question".$item['question_number'].$i;
					if($this->validation->$temp != FALSE)
					{
						$value = substr($this->validation->$temp, strpos($this->validation->$temp, " ") + 1, strlen($this->validation->$temp) );
						echo "<tr id=\"question".$item['question_number']."option".$i."\"><td>".$value."</td></tr>";
					}
			
					else
					{
						echo "<tr id=\"question".$item['question_number']."option".$i."\"><td>".$item['choices'][$i]."</td></tr>";
					}
		
			
					echo "<input type=\"hidden\" name=\"question".$item['question_number'].$i."\" id=\"question".$item['question_number'].$i."\" value=\"".$item['choices'][$i]."\">";
				}

				echo "</table></td></tr></table>";
				?>
		
				<script type="text/javascript" src="http://localhost/pollwatcher/public/js/jquery.tablednd_0_5.js"></script>
				<script type="text/javascript">
					$(document).ready(function() {
						$("#<?php echo "question".$item['question_number'];?>").tableDnD(
							{
								onDragClass: "myDragClass",
								onDrop: function(table, row) {
									var rows = table.tBodies[0].rows;
						
									for (var i=0; i<rows.length; i++) {
										$("input[name=<?php echo "question".$item['question_number']; ?>"+(i+1)+"]").val((i+1) + " " + $("#"+rows[i].id + " > td" ).text());
									}
								}
							}
						);
					});
					</script>
				<?php
		
			}
	
			if($item['with_text'] ==1)
			{
				$text_name = "text".$item['question_number'];
				echo "&nbsp;&nbsp;".$item['text_label'];
				$data = array(
				      'name'        => "text".$item['question_number'],
				      'id'          => "text".$item['question_number'],
				      'value'       => '',
				      'style'       => 'width:30%;margin:10px',
				    );
				echo form_input($data, $this->validation->$text_name);
			}
		
			echo "<br/><br/>";
		}
	}
	echo "</div>";
}


?>

<?php 
if($initially_empty == FALSE || sizeof($questions) > 5)
{
?>

	<a id="back">Back</a>
	<a id="next">Next</a> <br/>

	<script type="text/javascript">
	$(document).ready(function(){
		$("#back").hide();
		var current = 1;
		var limit = <?php echo sizeof($categories); ?>;
		$("#next").click( function(){
			if(current == 1)
			{
				$("#back").show();
			}
		
			if(current == limit - 1)
			{
				$("#next").hide();
			}
		
			if(current < limit)
			{
				$("#category"+ current).hide("slow");
				current = current + 1;
				$("#category"+ current).show("slow");
			}	
		});
	
		$("#back").click( function(){
			if(current == 2)
			{
				$("#back").hide();
			}
		
			if(current == limit)
			{
				$("#next").show();
			}
				
			if(current > 1)
			{
				$("#category"+ current).hide("slow");
				current = current - 1;
				$("#category"+ current).show("slow");
			}
					
		});
	});
	</script>
<?php
}
?>
<?php
	echo form_submit('submit', 'Submit answers');
	echo form_reset('reset', 'Reset');
	echo form_close();
?>
